package com.lfg.leetcode.leetcode100;

import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class No199rightSideView {

    public static void main(String[] args) {
        List<Integer> ret = rightSideView(TreeNode.stringToTreeNode("[1,2,]"));
        System.out.println(ret);
    }


    public static List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (null == root) {
            return ret;
        }
        List<TreeNode> children = new ArrayList<>();
        children.add(root);

        while (!children.isEmpty()) {
            ret.add(children.get(children.size() - 1).val);
            List<TreeNode> parents = new ArrayList<>();
            for (TreeNode node : children) {
                if (null != node.left) {
                    parents.add(node.left);
                }
                if (null != node.right) {
                    parents.add(node.right);
                }
            }
            children = parents;
        }
        return ret;
    }
}
